package com.junction.util;

import com.junction.CameraServerApplication;
import com.junction.pojo.TaskPojo;
import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicBoolean;


/**
 * @author ：liming
 * @date ：Created in 2022/5/5 14:08
 * @description：
 */
public class KeyHook {
    private static WinUser.HHOOK hhk;
    private static final AtomicBoolean stop = new AtomicBoolean(false);

    private static Logger logger = LoggerFactory.getLogger(KeyHook.class);



    public static void win32KeyHook() {
        User32 lib = User32.INSTANCE;
        WinDef.HMODULE  hmodule = Kernel32.INSTANCE.GetModuleHandle(null);
        LowLevelKeyboardProc lowLevelKeyboardProc = (nCode, wParam, lParam) -> {
            if (nCode >= 0) {
                //CTRL + A 屏蔽键盘
//                if (lParam.flags == 0 && lParam.vkCode == 65 && lib.GetAsyncKeyState(162) < 0) {
//                    stop.set(!stop.get());
//                } else {
//                    //lParam.vkCode != 162 阻止ctrl 一直处于按下状态
//                    if (stop.get() &&  lParam.vkCode != 162) {
//                        return new LRESULT(1);
//                    }
//                }

                logger.info("=========keyboard input: " +  keyCodeMyEnum.getName(lParam.vkCode));
            }
            return lib.CallNextHookEx(hhk, nCode, wParam, null);
        };
        hhk = lib.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lowLevelKeyboardProc, hmodule, 0);
        //当窗口关闭时，取消钩子
        WinUser.MSG msg = new WinUser.MSG();
        int result;
        while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
            if (result == -1) {
                break;
            }
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
        lib.UnhookWindowsHookEx(hhk);

    }




    public static void main(String[] args) {
        User32 lib = User32.INSTANCE;
        WinDef.HMODULE  hmodule = Kernel32.INSTANCE.GetModuleHandle(null);
        LowLevelKeyboardProc lowLevelKeyboardProc = (nCode, wParam, lParam) -> {
            if (nCode >= 0) {
                //CTRL + A 屏蔽键盘
                if (lParam.flags == 0 && lParam.vkCode == 65 && lib.GetAsyncKeyState(162) < 0) {
                    stop.set(!stop.get());
                } else {
                    //lParam.vkCode != 162 阻止ctrl 一直处于按下状态
                    if (stop.get() &&  lParam.vkCode != 162) {
                        return new LRESULT(1);
                    }
                }
            }
            return lib.CallNextHookEx(hhk, nCode, wParam, null);
        };
        hhk = lib.SetWindowsHookEx(User32.WH_KEYBOARD_LL, lowLevelKeyboardProc, hmodule, 0);
        //当窗口关闭时，取消钩子
        WinUser.MSG msg = new WinUser.MSG();
        int result;
        while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
            if (result == -1) {
                break;
            }
            lib.TranslateMessage(msg);
            lib.DispatchMessage(msg);
        }
        lib.UnhookWindowsHookEx(hhk);
    }

}

